package android.theHead;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class GetData {

	private static String convertStreamToString(InputStream is) {
        /*
         * To convert the InputStream to String we use the BufferedReader.readLine()
         * method. We iterate until the BufferedReader return null which means
         * there's no more data to read. Each line will appended to a StringBuilder
         * and returned as String.
         */
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
 
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
	
	public String connect(String url){
		String websiteData = null;
		
		try {
			DefaultHttpClient client = new DefaultHttpClient();
			URI uri = new URI(url);
			HttpGet method = new HttpGet(uri);
			HttpResponse res = client.execute(method);
			InputStream data = res.getEntity().getContent();
			websiteData = convertStreamToString(data);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return websiteData;
	}

//	
//	
//	
//	
//		HttpClient httpclient = new DefaultHttpClient();  
//	    HttpPost httppost = new HttpPost(url);
//	    
//	    Log.i("post", "connecting");
//	  
//	    try {  
//	        httppost.setEntity(new UrlEncodedFormEntity(data));  
//	  
//	        // Execute HTTP Post Request  
//	        HttpResponse response = httpclient.execute(httppost);
//	        
//	        Log.i("post", response.getStatusLine().toString());
//	        
//	        // get entity
//	        HttpEntity entity = response.getEntity();
//	        
//	        String result = null;
//	        
//            if (entity != null) {
//            	 
//                // A Simple JSON Response Read
//                InputStream instream = entity.getContent();
//                result= convertStreamToString(instream);
//                Log.i("post",result);
// 
//                // Closing the input stream will trigger connection release
//                instream.close();
//            }
//            return result;
//	        
//	    } catch (ClientProtocolException e) {  
//            Log.i("PostData:Client protocol",e.toString());
//	    } catch (IOException e) {
//	    	Log.i("PostData:IOException	",e.toString());
//	    }
//	    return null;
//	}
	
}
